package com.qkl.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qkl.user.dto.user.UserProblemDto;
import com.qkl.user.entity.Problem;
import com.qkl.user.entity.UserProblem;
import com.qkl.user.mapper.ProblemMapper;
import com.qkl.user.mapper.UserProblemMapper;
import com.qkl.user.service.UserProblemService;
import com.qkl.user.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class UserProblemServiceImpl implements UserProblemService {
    @Autowired
    private UserProblemMapper userProblemMapper;
    @Autowired
    private ProblemMapper problemMapper;
    @Override
    public UserProblemDto getUserProblem() {
        Long userId = JwtUtils.getUserId();
        List<Long> resultList = new ArrayList<>();
        QueryWrapper<UserProblem> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("problem_one_id", "problem_two_id");
        queryWrapper.eq("user_id", userId);
        UserProblem userProblem = userProblemMapper.selectOne(queryWrapper);

        resultList.add(Long.valueOf(userProblem.getProblemOneId()));
        resultList.add(Long.valueOf(userProblem.getProblemTwoId()));

        QueryWrapper<Problem> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.in("id", resultList);
        queryWrapper1.select("name");
        List<Problem> problemList = problemMapper.selectList(queryWrapper1);
        List<String> nameList = problemList.stream()
                .map(Problem::getName) // 提取 name 字段
                .toList(); // 收集成 List<String>
        UserProblemDto userProblemDto = new UserProblemDto();
        userProblemDto.setProblemOne(nameList.get(0));
        userProblemDto.setProblemTwo(nameList.get(1));
        return userProblemDto;
    }
}
